home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
recio202.zip
/
testchp.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-05-05
|
7KB
|
245 lines
/* testchp.c - tests character delimited put functions */
/* recio version 2.02, release May 5, 1994 */
/* Copyright (C) 1994 William Pierpoint */
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include "recio.h"
/* errors to stderr */
#define errout stdout
/****************************************************************************
Dynamic string concat function, kludged for use with recio. This function
is not part of recio nor was rseterr() designed to work with it, but you get
the idea.
Precondition: set dst to null ptr when declaring it, e.g. char *dst=NULL;
Syntax: the assignment operator is mandatory, i.e. dst=scats(dst, src);
Obligation: free dst when finished with it.
*****************************************************************************/
char * /* return dst */
scats( /* concat string dynamically */
char *dst, /* destination string pointer */
char *src) /* source string pointer */
/****************************************************************************/
{
size_t dlen; /* strlen of dst */
size_t slen; /* strlen of src */
/* if null src pointer or src empty, do nothing */
if (!src || !*src) {
goto done;
}
if (dst) {
dlen = strlen(dst);
slen = strlen(src);
do {
dst = (char *) realloc(dst, dlen+slen+1);
if (!dst) {
if (rseterr(NULL, ENOMEM)) goto done;
}
} while (!dst);
strcat(dst, src);
} else {
do {
dst = strdup(src);
if (!dst) {
if (rseterr(NULL, ENOMEM)) goto done;
}
} while (!dst);
}
done:
return dst;
}
enum {RESET, INCR, REPORT};
/****************************************************************************/
void /* return nothing */
warnttl( /* warning totals */
int action, /* action (0=reset, 1=increment, 2=report) */
int warnum) /* warning number */
/****************************************************************************/
{
static unsigned int widthcnt=0;
static unsigned int noregcnt=0;
switch (action) {
case RESET:
widthcnt=0;
return;
case INCR:
switch(warnum) {
case R_WWIDTH:
widthcnt++;
return;
case R_WNOREG:
noregcnt++;
return;
}
return;
case REPORT:
fprintf(errout, "\n");
if (widthcnt)
fprintf(errout, "WARNING -- %s -- %u times\n",
rstrwarning(R_WWIDTH), widthcnt);
if (noregcnt)
fprintf(errout, "WARNING -- %s\n", rstrwarning(R_WNOREG));
return;
}
}
/****************************************************************************/
void /* returns nothing */
rwarnfn( /* recio callback warning function */
REC *rp) /* record pointer */
/****************************************************************************/
{
if (risvalid(rp)) warnttl(INCR, rwarning(rp));
}
/****************************************************************************/
void /* returns nothing */
rerrfn( /* recio callback error function */
REC *rp) /* record pointer */
/****************************************************************************/
{
if (risvalid(rp)) {
/* determine cause of error */
switch (rerror(rp)) {
case R_ENOPUT: /* could not write data */
fprintf(errout, "ERROR %s -- %s\n", rnames(rp), rerrstr(rp));
break;
/* fatal errors (R_EINVMOD, R_EINVAL, R_ENOMEM) */
default:
fprintf(errout, "FATAL ERROR %s -- %s\n", rnames(rp), rerrstr(rp));
abort();
break;
}
/* invalid record pointer */
} else {
switch (errno) {
/* non-fatal errors */
case EACCES:
case EMFILE:
fprintf(errout, "WARNING: %s\n", strerror(errno));
break;
/* fatal errors (EINVAL, ENOMEM) */
default:
fprintf(errout, "FATAL ERROR: %s\n", strerror(errno));
abort();
break;
}
}
}
/****************************************************************************/
void putcolnumbers(void)
/****************************************************************************/
{
puts("");
puts(" 1 2 3 4 5 6");
puts("123456789012345678901234567890123456789012345678901234567890");
}
/****************************************************************************
main
*****************************************************************************/
int main()
{
int j, k; /* loop indices */
int i; /* integer field */
unsigned int ui; /* unsigned integer field */
long l; /* long field */
unsigned long ul; /* unsigned long field */
float f; /* float field */
double d; /* double field */
int ch; /* character field */
char *str = NULL; /* string field */
char str1[]="A"; /* string consisting of one letter */
/* install warning and error functions */
rsetwarnfn(rwarnfn);
rseterrfn(rerrfn);
/* if output not redirected */
if (isatty(fileno(stdout))) {
/* print instructions */
puts("TESTCHP version 2.02 Copyright (C) 1994 William Pierpoint");
puts("Tests recio character delimited put functions.");
}
for (k=0; k<=1; k++) {
switch (k) {
case 1:
rsetfldch(recout, ',');
rsettxtch(recout, '"');
break;
}
/* initialize data */
i = -1;
ui = 1;
l = -1L;
ul = 1L;
f = 1.111111;
d = 1.111111111111111111111;
ch = 'a';
if (str) *str='\0';
*str1 = toupper(ch);
str = scats(str, str1);
/* loop through data */
for (j=0; j<11; j++) {
rputi(recout, i);
rputui(recout, ui);
rputl(recout, l);
rputul(recout, ul);
rputf(recout, f);
rputd(recout, d);
rputc(recout, ch);
rputs(recout, str);
i *= 10;
ui *= 10;
l *= 10L;
ul *= 10L;
f *= 10.;
d *= 10.;
ch += 1;
*str1 = toupper(ch);
str = scats(str, str1);
rputrec(recout);
}
}
/* free string fields */
free (str);
/* check stream for warnings */
if (rwarning(recout)) warnttl(REPORT, NULL);
/* check stream for error */
if (rerror(recout)) {
fprintf(stdout, "\nERROR %s -- %s\n",
rnames(recout), rerrstr(recout));
exit (EXIT_FAILURE);
}
return EXIT_SUCCESS;
}